﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-25 15:33:16
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 ORDERFROM
    /// </summary>
    public class ORDERFROM : IORDERFROM
    {
        public ORDERFROM()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.ORDERFROM model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@OF_CODE", SqlDbType.VarChar,20),
                    new SqlParameter("@OF_NAME", SqlDbType.VarChar,100),
                    new SqlParameter("@OF_ACODE", SqlDbType.VarChar,100),
                    new SqlParameter("@OF_MYSHOP", SqlDbType.SmallInt),
                    new SqlParameter("@OF_STOP", SqlDbType.SmallInt),
                    new SqlParameter("@OF_NOTE", SqlDbType.VarChar,200),
                    new SqlParameter("@OF_FIELD1", SqlDbType.VarChar,20),
                    new SqlParameter("@OF_FIELD2", SqlDbType.VarChar,20)
            };
            parameters[0].Value = model.OF_CODE;
            parameters[1].Value = model.OF_NAME;

            if (model.OF_ACODE != null)
                parameters[2].Value = model.OF_ACODE;
            else
                parameters[2].Value = DBNull.Value;
            
            parameters[3].Value = model.OF_MYSHOP;
            parameters[4].Value = model.OF_STOP;
            
            if (model.OF_NOTE != null)
                parameters[5].Value = model.OF_NOTE;
            else
                parameters[5].Value = DBNull.Value;


            if (model.OF_FIELD1 != null)
                parameters[6].Value = model.OF_FIELD1;
            else
                parameters[6].Value = DBNull.Value;


            if (model.OF_FIELD2 != null)
                parameters[7].Value = model.OF_FIELD2;
            else
                parameters[7].Value = DBNull.Value;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_ADD", parameters, out rowsAffected);

            if (rowsAffected > 0)
                return model.OF_CODE;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ORDERFROM model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@OF_CODE", SqlDbType.VarChar,20),
                    new SqlParameter("@OF_NAME", SqlDbType.VarChar,100),
                    new SqlParameter("@OF_ACODE", SqlDbType.VarChar,100),
                    new SqlParameter("@OF_MYSHOP", SqlDbType.SmallInt),
                    new SqlParameter("@OF_STOP", SqlDbType.SmallInt),
                    new SqlParameter("@OF_NOTE", SqlDbType.VarChar,200),
                    new SqlParameter("@OF_FIELD1", SqlDbType.VarChar,20),
                    new SqlParameter("@OF_FIELD2", SqlDbType.VarChar,20)
            };
            parameters[0].Value = model.OF_CODE;
            parameters[1].Value = model.OF_NAME;

            if (model.OF_ACODE != null)
                parameters[2].Value = model.OF_ACODE;
            else
                parameters[2].Value = DBNull.Value;

            parameters[3].Value = model.OF_MYSHOP;
            parameters[4].Value = model.OF_STOP;

            if (model.OF_NOTE != null)
                parameters[5].Value = model.OF_NOTE;
            else
                parameters[5].Value = DBNull.Value;


            if (model.OF_FIELD1 != null)
                parameters[6].Value = model.OF_FIELD1;
            else
                parameters[6].Value = DBNull.Value;


            if (model.OF_FIELD2 != null)
                parameters[7].Value = model.OF_FIELD2;
            else
                parameters[7].Value = DBNull.Value;


            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_Update", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string OF_CODE)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@OF_CODE", SqlDbType.VarChar,20)};
            parameters[0].Value = OF_CODE;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_Delete", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string OF_CODE)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@OF_CODE", SqlDbType.VarChar,20)};
            parameters[0].Value = OF_CODE;
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_Exists", parameters, out rowsAffected) == 1;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ORDERFROM GetModel(string OF_CODE)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@OF_CODE", SqlDbType.VarChar,20)};
            parameters[0].Value = OF_CODE;
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.ORDERFROM> GetList()
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_GetList", parameters, "ds");
            return GetList(ds);
        }
        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.ORDERFROM> GetList(bool yx)
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_ORDERFROM_GetList_YX", parameters, "ds");
            return GetList(ds);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.ORDERFROM> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[ORDERFROM]", null, pageSize, pageIndex, fldSort, sort, strCondition, "OF_CODE", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.ORDERFROM GetModel(DataRow r)
        {
            Model.ORDERFROM model = new Model.ORDERFROM();
            model.OF_CODE = SqlHelper.GetString(r["OF_CODE"]);
            model.OF_NAME = SqlHelper.GetString(r["OF_NAME"]);
            model.OF_ACODE = SqlHelper.GetString(r["OF_ACODE"]);
            model.OF_MYSHOP = SqlHelper.GetInt(r["OF_MYSHOP"]);
            model.OF_STOP = SqlHelper.GetInt(r["OF_STOP"]);
            model.OF_NOTE = SqlHelper.GetString(r["OF_NOTE"]);
            model.OF_FIELD1 = SqlHelper.GetString(r["OF_FIELD1"]);
            model.OF_FIELD2 = SqlHelper.GetString(r["OF_FIELD2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.ORDERFROM> GetList(DataSet ds)
        {
            List<Model.ORDERFROM> l = new List<Model.ORDERFROM>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
